/**
 * Copyright (C), 2015, 上海赛可电子商务有限公司
 * Author:   张卫明
 * Date:     2015-4-28
 * lever:    page
 * Description: 首页js
 */

;(function($,window) {

	listPage = {

		init:function(){

			//搜索条件 伸缩
			this.filterMore();

			//加tooltip
			ECar.tooltip();

			//图片懒加载
			$("img.lazy").lazyload({effect: "fadeIn",failure_limit:10});

		            //搜索无结果 提交反馈
		            this.comp();
		},
		filterMore:function(){
			var $filterBox = $('.filter-box'),$filterBox_a = $('a','.filter-box').eq(0),_this = this;
			if($filterBox.length <= 0){return false};
			var $filterHei = $filterBox_a.outerHeight() + $filterBox_a.css("marginBottom").replace('px','')*1 + $filterBox_a.css("marginTop").replace('px','')*1;

			//初始化每项 搜索类表
			$filterBox.each(function(){
				var _thisH = $(this).height();
				var _thisItem = $(this).parents('.filter-item');
				var _thisMore = _thisItem.find('.filter-more');

				if(_thisH>=$filterHei*2){
					_thisMore.show();
				}

				//给更多绑定 伸缩事件
				_thisMore.on('click',function(){
					_this._filterMore(_thisItem,this);
				});

			});

		},
		_filterMore:function(_thisItem,_this){

			if(!_thisItem.hasClass('Multi-item')){
				_thisItem.addClass('Multi-item');
				$(_this).find('i').attr('class','arraw-v arraw-up');
			} else {
				_thisItem.removeClass('Multi-item');
				$(_this).find('i').attr('class','arraw-v arraw-down');
			}
		},
		comp:function(){
            if($('#search-comp').length <= 0){
                return false;
            };

            $('#search-comp').on('click',function(){
                sidebar.openFeedbook();
            });
        }
	};

	listPage.init();
	var priceRange = {
			self:'',
			priceElm : $('.price-range'),
			fromPrice : $('#fromPrice'),
			toPrice : $('#toPrice'),
			priceBtn : $('.submit'),
			priceHover : function(){
				var self = this;
				self.priceElm.hover(function(){
					self.priceElm.addClass('hit');
				},function(){
					self.priceElm.removeClass('hit');
				});
			},
			warning : function(element){
				element.css("border-color","#c71445");
			},
			//重置颜色
			reset : function(){
				$("#fromPrice").css("border-color","#dfdfdf");
				$("#toPrice").css("border-color","#dfdfdf");
			},
			priceSubmit : function(){
				var self = this;
				self.priceBtn.click(function(){
					var fromPrice = parseInt(self.fromPrice.val());
					var toPrice = parseInt(self.toPrice.val());
					var bool = false;
					//判断输入的值是否是数值类型
					if(isNaN(fromPrice)){
						self.warning($('#fromPrice'));
						bool = true;
					}
					if($.trim(self.toPrice.val()).length > 0 && isNaN(toPrice)){
						self.warning($('#toPrice'));
						bool = true;
					}
					if(bool)
						return;
					//价格必须低到高
					if(fromPrice < 0 || fromPrice < 0 || (toPrice < fromPrice)){
						self.warning($('#fromPrice'));
						self.warning($('#toPrice'));
						return;
					}else{
						self.reset();
					}
					var loc = window.location.href;
					//重新定义href
					if(loc.lastIndexOf(".html") > -1){
						loc = loc.substring(0,loc.lastIndexOf(".html")+".html".length);
					}
					if(isNaN(toPrice)){
						toPrice = "";
					}
					window.location.href = loc + "?minBarePrice=" + fromPrice + "&maxBarePrice=" + toPrice;
				});
			}
		};
	priceRange.priceHover();
	priceRange.priceSubmit();
})(jQuery,window);

